/******************************************************************************

    Simple Authentication and Authorisation System (SAAS)

    instance.c

    Copyright (C) 2011  Josh Goes


    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    If you require a mailing address for this software, please send your
    mail to the following address:
        SAAS
        GPO Box 2973
        BRISBANE QLD 4001
        AUSTRALIA

******************************************************************************/

#include "saas-build.h"

#include <string.h>


uint16_t saas_init( saas_t *inst , const char *conf_file , uint8_t log_level , uint8_t log_type , FILE *log_file , void *log_handle )
{
	memset( inst , 0 , sizeof( saas_t ) );

	// set the default options.
	inst->log_type           = SAAS_LOG_SYSLOG;
	inst->host_order         = SAAS_HOST_ORDER_SEQ;
	inst->dns_prefer_ipv6    = 0;
	inst->use_ipv4           = 1;
	inst->use_ipv6           = 0;
	inst->timeout            = 3000; // ms
	inst->key_time_tolerance = 5;
	inst->time_adjust        = 0;


	// set up the logging system
	switch ( log_type )
	{
		case SAAS_LOG_NONE:
		case SAAS_LOG_SYSLOG:
			break;

		case SAAS_LOG_STDOUT:
			inst->log_file = stdout;
			break;

		case SAAS_LOG_STDERR:
			inst->log_file = stderr;
			break;

		case SAAS_LOG_FILE:
			inst->log_file = log_file;
			break;

		case SAAS_LOG_PAM_SYSLOG:
			inst->log_handle = log_handle;
			break;

		default:
			return SAAS_OP_ARG_ERROR;
	}
	inst->log_type = log_type;

	if ( log_level > 7 )
		return SAAS_OP_ARG_ERROR;
	inst->log_level = log_level;


	// now call the configuration file parser.
	if ( conf_file != NULL )
		return _conf_parse( inst , conf_file );

	return _conf_parse( inst , SAAS_CONF_PATH_DEFAULT );
}



uint16_t saas_free( saas_t *inst )
{
	memset( inst , 0 , sizeof( saas_t ) );
	return SAAS_OP_SUCCESS;
}


void saas_dump( saas_t *inst , const char *line_prefix )
{
	dump_hosts( inst , LOG_DEBUG , line_prefix );
	logm( inst , LOG_DEBUG , "%shost_order = %d" , line_prefix , inst->host_order );
	logm( inst , LOG_DEBUG , "%sdns_prefer_ipv6 = %d" , line_prefix , inst->dns_prefer_ipv6 );
	logm( inst , LOG_DEBUG , "%suse_ipv4 = %d" , line_prefix , inst->use_ipv4 );
	logm( inst , LOG_DEBUG , "%suse_ipv6 = %d" , line_prefix , inst->use_ipv6 );
	logm( inst , LOG_DEBUG , "%stimeout = %d" , line_prefix , inst->timeout );
	logm( inst , LOG_DEBUG , "%skey_time_tolerance = %u" , line_prefix , inst->key_time_tolerance );
	logm( inst , LOG_DEBUG , "%stime_adjust = %d" , line_prefix , inst->time_adjust );
	logm( inst , LOG_DEBUG , "%ssecret_len = %d" , line_prefix , inst->secret_len );
	logm( inst , LOG_DEBUG , "%ssecret = %s" , line_prefix , inst->secret );
}

